import asyncio


async def handle_client(reader, writer):
    # 从客户端读取数据
    while True:
        data = await reader.read(100)
        if not data:
            break
        message = data.decode()
        addr = writer.get_extra_info('peername')
        print(f"Received {message} from {addr}")

    # # 发送响应给客户端
    # response = f"Hello {addr}, I received your message: {message}"
    # writer.write(response.encode())
    # await writer.drain()

    # 关闭连接
    writer.close()


async def main():
    # 创建一个TCP服务器
    server = await asyncio.start_server(
        handle_client, '192.168.90.164', 18080)

    # 获取服务器地址
    addr = server.sockets[0].getsockname()
    print(f'Serving on {addr}')

    # 等待连接
    async with server:
        await server.serve_forever()


asyncio.run(main())
